/**
 * 2017年3月9日
 */
package cn.edu.bjtu.alex.rewrite.components;

import java.util.Map;
import java.util.Map.Entry;

import cn.edu.bjtu.alex.rewrite.Context;
import cn.edu.bjtu.alex.rewrite.interfaces.DocumentAnalyzer;
import cn.edu.bjtu.alex.rewrite.interfaces.Term;
import cn.edu.bjtu.alex.rewrite.tools.LuceneDocumentAnalyzer;
import cn.edu.bjtu.entity.IDocument;

/**
 * @author Alex
 *
 */
public class TFModelTransformer extends BaseComponent {

	/**
	 * @param ctx
	 */
	public TFModelTransformer(Context ctx) {
		super(ctx);
	}

	protected void transformInternal() {
		DocumentAnalyzer analyzer = new LuceneDocumentAnalyzer(ctx);
		ctx.clearSegmentedDocuments();
		while(ctx.getDocuments().size()>0){
			IDocument d = ctx.getDocument();
			Map<String, Term> terms = analyzer.analyze(d.getContent());
			double totalWord = terms.size();
			for(Entry<String,Term> term:terms.entrySet()){
				Term t = term.getValue();
				
				t.setTf(t.getFreq()/totalWord);
			}
			ctx.addTransformedDocument(d, terms);
		}
	}

	protected void fitInternal() {}

}
